package com.example.uupaymuch.controller.channel.eastpay;

import com.example.uupaymuch.controller.notice.east.EastTransferComponent;
import com.example.uupaymuch.controller.notice.wudi.TransferComponent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
public class EastTransferController {

    @Autowired
    private EastTransferComponent eastTransferComponent;

    private final Random random = new Random();
    private final double successProbability = 0.5; // 50% 的成功概率

    @PostMapping("/gateway/v1.0/cashOut/submit")
    public ResponseEntity<Map<String, Object>> getTestTransaction(@RequestBody Map<String, Object> request) {

        // 生成一个随机数，并与成功概率进行比较
        boolean isSuccess = random.nextDouble() < successProbability;
        // 模拟入参
        String partnerTrxId = request.get("partner_trx_id") != null ? (String) request.get("partner_trx_id") : "";
        String amount = request.get("amount") != null ? (String) request.get("amount") : "";


        Map<String, Object> response = new HashMap<>();
        Integer payStatus = 0;

        // 创建数据部分
        Map<String, Object> data = new HashMap<>();
        data.put("amount", amount);
        data.put("charging_fees_method", 1);
        data.put("plat_fee", 2000.00);
        data.put("partner_trx_id", partnerTrxId);
        data.put("status", 0);
        data.put("plat_trx_id", "CA1746075716631531520");
        data.put("status_msg", "Pending process");

        // 将数据部分添加到响应中
        response.put("data", data);
        response.put("code", "000");
        response.put("message", "Successful");

        payStatus = isSuccess ? 2 : 4;

        // 回调
        eastTransferComponent.doNotifyTransfer(partnerTrxId, payStatus, amount);

        // 返回响应
        return ResponseEntity.ok(response);
    }
}